package com.muyu.cloud.auth.controller;

import com.muyu.cloud.auth.domain.request.UserLoginReq;
import com.muyu.cloud.auth.domain.response.TokenRes;
import com.muyu.cloud.auth.service.AuthService;
import com.muyu.cloud.system.domain.UserInfo;
import com.muyu.common.constant.JwtConstants;
import com.muyu.common.constant.TokenConstants;
import com.muyu.common.result.Result;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: DongZeLiang
 * @date: 2024/5/24
 * @Description: 授权服务
 * @Version: 1.0
 */
@RestController
@AllArgsConstructor
public class AuthController {

    /**
     * 登录业务层
     */
    private final AuthService authService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<TokenRes> login(@RequestBody @Validated UserLoginReq userLoginReq) {
        UserInfo userLoginInfo = authService.login(userLoginReq.getUserName(), userLoginReq.getPassword());
        TokenRes tokenRes = authService.genToken(userLoginInfo);
        return Result.success(tokenRes);
    }
    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated UserLoginReq userLoginReq) {
        Result result = authService.register(userLoginReq);
        return result;
    }

    @GetMapping("/login/info")
    public Result<UserInfo> getUserInfo(@RequestHeader(JwtConstants.USER_KEY) String userKey) {
        return Result.success(
                authService.loginInfo(userKey)
        );
    }

    /**
     * 注销
     */
    @GetMapping("/login-out")
    public Result<String> loginOut(@RequestHeader(JwtConstants.USER_KEY) String userKey){
        authService.loginOut(userKey);
        return Result.success();
    }
}
